package controllers

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"math"
	"net/url"
	"nspel/entity"
	"nspel/model"
	"nspel/nspel"
	"nspel/util"
	"strconv"
)

type StudentsManageController struct {
	beego.Controller
}

func (c *StudentsManageController) PostStudentsImportCheck() {
	/*
		c.EnableRender = false
		requestData := new(model.PostSchoolsImport)
		err := requestData.Validate(&c.Controller)
		if err != nil {
			nspel.HttpError(err, &c.Controller, 400)
			return
		}
		excelData, err := util.TryParseXlsx(&requestData.ImportFile)
		if err != nil {
			nspel.HttpError(err, &c.Controller, 400)
			return
		}
		msg, _, _, _, err := util.ValidateStudentImport(&excelData)
		if err != nil {
			nspel.HttpError(err, &c.Controller, 500)
			return
		}
		c.Data["json"] = msg
		c.ServeJSON()
	*/
}

func (c *StudentsManageController) PostStudentsImportSubmit() {
	/*
		c.EnableRender = false
		requestData := new(model.PostSchoolsImport)
		err := requestData.Validate(&c.Controller)
		if err != nil {
			nspel.HttpError(err, &c.Controller, 400)
			return
		}
		excelData, err := util.TryParseXlsx(&requestData.ImportFile)
		if err != nil {
			nspel.HttpError(err, &c.Controller, 400)
			return
		}
		msg, create, update, schoolIdMap, err := util.ValidateStudentImport(&excelData)
		if err != nil {
			nspel.HttpError(err, &c.Controller, 500)
			return
		}
		if msg.Result {
			err = util.StudentImport(&create, &update, &schoolIdMap)
			if err != nil {
				nspel.HttpError(err, &c.Controller, 500)
				return
			}
		}
		c.Data["json"] = msg
		c.ServeJSON()
	*/
}

func (c *StudentsManageController) GetStudentsList() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	res := model.GetStudentsListRes{RetCode: 0}
	var count int64
	var err error
	var students []entity.Student
	if authority == entity.ManagerAuthority.Administrator {
		req := model.GetStudentsListAdmin{}
		b := req.Validate("province-id", "city-id", "area-id", "school-name", "join-year", "school-roll-id", "page-size", "current-page", &c.Controller)
		if !b {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		count, students, err = util.GetStudentsListAdministrator(req.ProvinceId, req.CityId, req.AreaId, req.SchoolName, req.JoinYear, req.SchoolRollId, req.PageSize, req.CurrentPage)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Total = int64(count)
	} else if authority == entity.ManagerAuthority.Area {
		req := model.GetStudentsListArea{}
		b := req.Validate("join-year", "school-id", "school-roll-id", "page-size", "current-page", &c.Controller)
		if !b {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		count, students, err = util.GetStudentsListArea(area.Id, req.JoinYear, req.SchoolId, req.SchoolRollId, req.PageSize, req.CurrentPage)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Total = int64(count)
	} else if authority == entity.ManagerAuthority.School {
		req := model.GetStudentsListSchool{}
		b := req.Validate("school-roll-id", "page-size", "current-page", &c.Controller)
		if !b {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		school := new(entity.School)
		err = school.SelectAllByUserId(uid)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		count, students, err = util.GetStudentsListSchool(school.Id, req.SchoolRollId, req.PageSize, req.CurrentPage)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Total = int64(count)
	}
	res.Data = make([]model.StudentsList, len(students))
	for i := 0; i < len(students); i++ {
		res.Data[i].Id = students[i].Id
		res.Data[i].SchoolRollId = students[i].SchoolRollId
		res.Data[i].Name = students[i].Name
		school := new(entity.School)
		err = school.SelectAllById(students[i].SchoolId)
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
		res.Data[i].SchoolName = school.Name
		res.Data[i].Sex = util.GetSex(students[i].Sex)
		res.Data[i].Grade = util.GetGrade(students[i].CurrentGrade, students[i].CurrentClass)
		res.Data[i].Class = util.GetClass(students[i].CurrentClass)
		res.Data[i].Age = util.GetAge(students[i].Birthday)
		score := new(entity.Score)
		err = score.SelectAllBySchoolRollIdLastestMeasurementDate(students[i].SchoolRollId)
		if err != nil {
			if err != orm.ErrNoRows {
				c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
				nspel.Error(err)
				return
			} else {
				res.Data[i].Score = ""
			}
		} else {
			res.Data[i].Score = strconv.FormatFloat(score.PhysicalTotalScore.Float64, 'f', 2, 64)
		}
		res.Data[i].Birthday = util.GetDateString(students[i].Birthday, "年", "月", "日", "")
		res.Data[i].IdentityId = students[i].IdentityId.String
		res.Data[i].Nation = util.GetNation(students[i].Nation)
		res.Data[i].Address = students[i].Address.String
		res.Data[i].Phone = students[i].Phone.String
		res.Data[i].StudentId = students[i].StudentId
	}
	c.Data["json"] = res
}

func (c *StudentsManageController) PostSchoolRollIdValidateExist() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	req := model.PostSchoolRollIdValidateExist{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	b, err := req.Validate(uid, authority)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if !b {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	student := new(entity.Student)
	err = student.SelectAllBySchoolRollId(req.SchoolRollId)
	if err != nil {
		if err == orm.ErrNoRows {
			c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
		} else {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	} else {
		if student.Id != req.Id {
			c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "与其他学籍号重复"}
		} else {
			c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
		}
	}
}

func (c *StudentsManageController) PostStudentPhoneExist() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	req := model.PostStudentPhoneExist{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	b, err := req.Validate(uid, authority)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if !b {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	student := new(entity.Student)
	err = student.SelectAllByPhone(req.Phone)
	if err != nil {
		if err == orm.ErrNoRows {
			c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
		} else {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	} else {
		if student.Id != req.Id {
			c.Data["json"] = map[string]interface{}{"retCode": 0, "result": false, "msg": "与其他电话重复"}
		} else {
			c.Data["json"] = map[string]interface{}{"retCode": 0, "result": true}
		}
	}
}

func (c *StudentsManageController) PostStudentModify() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	req := model.PostStudentModify{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	b, err := req.Validate(uid, authority)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if !b {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	student := new(entity.Student)
	err = student.SelectAllById(req.Id)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if req.SchoolRollId != student.SchoolRollId {
		student := new(entity.Student)
		err := student.SelectAllBySchoolRollId(req.SchoolRollId)
		if err != nil && err != orm.ErrNoRows {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		} else if err == nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
	}
	student.SchoolRollId = req.SchoolRollId
	student.Name = req.Name
	if req.Name == "男" {
		student.Sex = 1
	} else if req.Name == "女" {
		student.Sex = 2
	}
	student.Nation = req.Nation
	student.Birthday = req.Birthday
	student.IdentityId = util.GetNullString(req.IdentityId)
	school := new(entity.School)
	err = school.SelectAllById(student.Id)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if len(req.Class) == 1 {
		req.Class = "0" + req.Class
	}
	if entity.SchoolTypeMapCode[school.SchoolType] == "小" {
		if req.Grade[0:1] != "1" {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		student.CurrentClass = strconv.FormatInt(req.GradeYear, 10) + "1" + req.Class
	} else if entity.SchoolTypeMapCode[school.SchoolType] == "初" {
		if req.Grade[0:1] != "2" {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		student.CurrentClass = strconv.FormatInt(req.GradeYear, 10) + "2" + req.Class
	} else if entity.SchoolTypeMapCode[school.SchoolType] == "高" {
		if req.Grade[0:1] != "3" {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		}
		student.CurrentClass = strconv.FormatInt(req.GradeYear, 10) + "3" + req.Class
	} else {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	student.CurrentGrade = req.Grade
	student.StudentId = req.StudentId
	student.Phone = util.GetNullString(req.Phone)
	student.Address = util.GetNullString(req.Address)
	err = student.Update("school_roll_id", "name", "sex", "nation", "birthday", "identity_id", "current_grade", "current_class", "student_id", "phone", "address")
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	nspel.ResdisKeysDelete("student")
	c.Data["json"] = map[string]interface{}{"retCode": 0, "msg": "更新成功"}
}

func (c *StudentsManageController) PostStudentAdd() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	req := model.PostStudentAdd{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	b, err := req.Validate(uid, authority)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if !b {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	student := new(entity.Student)
	student.CountryId = req.CountryId
	student.ProvinceId = req.ProvinceId
	student.CityId = req.CityId
	student.AreaId = req.AreaId
	student.ExperimentAreaId = req.ExperimentAreaId
	student.SchoolId = req.SchoolId
	student.SchoolRollId = req.SchoolRollId
	student.Name = req.Name
	if req.Sex == "男" {
		student.Sex = 1
	} else if req.Sex == "女" {
		student.Sex = 2
	}
	student.Nation = req.Nation
	student.Birthday = req.Birthday
	student.IdentityId = util.GetNullString(req.IdentityId)
	student.CurrentGrade = req.Grade
	if len(req.Class) == 1 {
		req.Class = "0" + req.Class
	}
	student.CurrentClass = strconv.FormatInt(req.GradeYear, 10) + req.Grade[0:1] + req.Class
	student.StudentId = req.StudentId
	student.Phone = util.GetNullString(req.Phone)
	student.Address = util.GetNullString(req.Address)
	err = student.Insert()
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	c.Data["json"] = map[string]interface{}{"retCode": 0, "msg": "新增成功"}
	nspel.ResdisKeysDelete("student")
}

func (c *StudentsManageController) DeleteStudent() {
	defer c.ServeJSON()
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Data["json"] = model.ErrorRes{RetCode: 1, Msg: "session失效"}
		return
	}
	req := model.DeleteStudent{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
		return
	}
	b, err, msg := req.Validate(uid, authority)
	if err != nil {
		c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
		nspel.Error(err)
		return
	}
	if !b {
		if len(msg) == 0 {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "请求无效"}
			return
		} else {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: msg}
			return
		}
	}
	for i := 0; i < len(req.Id); i++ {
		student := new(entity.Student)
		err = student.DeleteById(req.Id[i])
		if err != nil {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
			return
		}
	}
	nspel.ResdisKeysDelete("student")
	c.Data["json"] = map[string]interface{}{"retCode": 0, "msg": "删除成功"}
}

func (c *StudentsManageController) GetStudentExport() {
	c.EnableRender = false
	authority, ok := c.Ctx.Input.Session("authority").(byte)
	if !ok {
		c.Ctx.ResponseWriter.Status = 403
		return
	}
	uid, ok := c.Ctx.Input.Session("uid").(int64)
	if !ok {
		c.Ctx.ResponseWriter.Status = 403
		return
	}
	var err error
	var students []entity.Student
	if authority == entity.ManagerAuthority.Administrator {
		req := model.GetStudentExportAdmin{}
		b := req.Validate("province-id", "city-id", "area-id", "school-name", "join-year", "school-roll-id", &c.Controller)
		if !b {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
		_, students, err = util.GetStudentsListAdministrator(req.ProvinceId, req.CityId, req.AreaId, req.SchoolName, req.JoinYear, req.SchoolRollId, math.MaxInt64, 1)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	} else if authority == entity.ManagerAuthority.Area {
		req := model.GetStudentExportArea{}
		b := req.Validate("join-year", "school-id", "school-roll-id", &c.Controller)
		if !b {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
		area := new(entity.Area)
		err = area.SelectAllByUserId(uid)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		_, students, err = util.GetStudentsListArea(area.Id, req.JoinYear, req.SchoolId, req.SchoolRollId, math.MaxInt64, 1)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	} else if authority == entity.ManagerAuthority.School {
		req := model.GetStudentExportSchool{}
		b := req.Validate("school-roll-id", &c.Controller)
		if !b {
			c.Ctx.ResponseWriter.Status = 400
			return
		}
		school := new(entity.School)
		err = school.SelectAllByUserId(uid)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		_, students, err = util.GetStudentsListSchool(school.Id, req.SchoolRollId, math.MaxInt64, 1)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
	}
	xlsx := excelize.NewFile()
	widthMap := make(map[string]float64)
	xlsx.SetSheetName("Sheet1", "学生")
	xlsx.SetCellValue("学生", "A1", "学籍号")
	widthMap["A"] = math.Max(widthMap["A"], util.GetStrLength("学籍号")+1)
	xlsx.SetCellValue("学生", "B1", "学校编码")
	widthMap["B"] = math.Max(widthMap["B"], util.GetStrLength("学校编码")+1)
	xlsx.SetCellValue("学生", "C1", "年级编码")
	widthMap["C"] = math.Max(widthMap["C"], util.GetStrLength("年级编码")+1)
	xlsx.SetCellValue("学生", "D1", "班级编码")
	widthMap["D"] = math.Max(widthMap["D"], util.GetStrLength("班级编码")+1)
	xlsx.SetCellValue("学生", "E1", "班级名称")
	widthMap["E"] = math.Max(widthMap["E"], util.GetStrLength("班级名称")+1)
	xlsx.SetCellValue("学生", "F1", "姓名")
	widthMap["F"] = math.Max(widthMap["F"], util.GetStrLength("姓名")+1)
	xlsx.SetCellValue("学生", "G1", "性别")
	widthMap["G"] = math.Max(widthMap["G"], util.GetStrLength("性别")+1)
	xlsx.SetCellValue("学生", "H1", "出生日期")
	widthMap["H"] = math.Max(widthMap["H"], util.GetStrLength("出生日期")+1)
	xlsx.SetCellValue("学生", "I1", "身份证号")
	widthMap["I"] = math.Max(widthMap["I"], util.GetStrLength("身份证号")+1)
	xlsx.SetCellValue("学生", "J1", "民族")
	widthMap["J"] = math.Max(widthMap["J"], util.GetStrLength("民族")+1)
	xlsx.SetCellValue("学生", "K1", "家庭住址")
	widthMap["K"] = math.Max(widthMap["K"], util.GetStrLength("家庭住址")+1)
	xlsx.SetCellValue("学生", "L1", "家长电话")
	widthMap["L"] = math.Max(widthMap["L"], util.GetStrLength("家长电话")+1)
	xlsx.SetCellValue("学生", "M1", "学号")
	widthMap["M"] = math.Max(widthMap["M"], util.GetStrLength("学号")+1)
	for i := 0; i < len(students); i++ {
		xlsx.SetCellValue("学生", "A"+strconv.Itoa(i+2), students[i].SchoolRollId)
		widthMap["A"] = math.Max(widthMap["A"], util.GetStrLength(students[i].SchoolRollId)+1)
		school := new(entity.School)
		err = school.SelectAllById(students[i].SchoolId)
		if err != nil {
			c.Ctx.ResponseWriter.Status = 500
			nspel.Error(err)
			return
		}
		xlsx.SetCellValue("学生", "B"+strconv.Itoa(i+2), school.SchoolCode)
		widthMap["B"] = math.Max(widthMap["B"], util.GetStrLength(school.SchoolCode)+1)
		xlsx.SetCellValue("学生", "C"+strconv.Itoa(i+2), students[i].CurrentGrade)
		widthMap["C"] = math.Max(widthMap["C"], util.GetStrLength(students[i].CurrentGrade)+1)
		xlsx.SetCellValue("学生", "D"+strconv.Itoa(i+2), students[i].CurrentClass)
		widthMap["D"] = math.Max(widthMap["D"], util.GetStrLength(students[i].CurrentClass)+1)
		var className string
		if students[i].CurrentClass[4:5] == "1" {
			className += "小学"
		} else if students[i].CurrentClass[4:5] == "2" {
			className += "初中"
		} else if students[i].CurrentClass[4:5] == "3" {
			className += "高中"
		}
		className += students[i].CurrentClass[0:4] + "级"
		if students[i].CurrentClass[5:6] == "0" {
			className += students[i].CurrentClass[6:7] + "班"
		} else {
			className += students[i].CurrentClass[5:7] + "班"
		}
		xlsx.SetCellValue("学生", "E"+strconv.Itoa(i+2), className)
		widthMap["E"] = math.Max(widthMap["E"], util.GetStrLength(className)+1)
		xlsx.SetCellValue("学生", "F"+strconv.Itoa(i+2), students[i].Name)
		widthMap["F"] = math.Max(widthMap["F"], util.GetStrLength(students[i].Name)+1)
		if students[i].Sex == 1 {
			xlsx.SetCellValue("学生", "G"+strconv.Itoa(i+2), "男")
		} else if students[i].Sex == 2 {
			xlsx.SetCellValue("学生", "G"+strconv.Itoa(i+2), "女")
		}
		widthMap["G"] = math.Max(widthMap["G"], util.GetStrLength("男")+1)
		if !students[i].Birthday.IsZero() {
			xlsx.SetCellValue("学生", "H"+strconv.Itoa(i+2), students[i].Birthday.Format("2006/01/02"))
			widthMap["H"] = math.Max(widthMap["H"], util.GetStrLength(students[i].Birthday.Format("2006/01/02"))+1)
		}
		xlsx.SetCellValue("学生", "I"+strconv.Itoa(i+2), students[i].IdentityId.String)
		widthMap["I"] = math.Max(widthMap["I"], util.GetStrLength(students[i].IdentityId.String)+1)
		xlsx.SetCellValue("学生", "J"+strconv.Itoa(i+2), students[i].Nation)
		widthMap["J"] = math.Max(widthMap["J"], util.GetStrLength(students[i].Nation)+1)
		xlsx.SetCellValue("学生", "K"+strconv.Itoa(i+2), students[i].Address.String)
		widthMap["K"] = math.Max(widthMap["K"], util.GetStrLength(students[i].Address.String)+1)
		xlsx.SetCellValue("学生", "L"+strconv.Itoa(i+2), students[i].Phone.String)
		widthMap["L"] = math.Max(widthMap["L"], util.GetStrLength(students[i].Phone.String)+1)
		xlsx.SetCellValue("学生", "M"+strconv.Itoa(i+2), students[i].StudentId)
		widthMap["M"] = math.Max(widthMap["M"], util.GetStrLength(students[i].StudentId)+1)
	}
	for k, v := range widthMap {
		xlsx.SetColWidth("学生", k, k, v)
	}
	buffer, err := xlsx.WriteToBuffer()
	if err != nil {
		c.Ctx.ResponseWriter.Status = 500
		nspel.Error(err)
		return
	}
	c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape("学生.xlsx"))
	c.Ctx.Output.Header("Content-Description", "File Transfer")
	c.Ctx.Output.Header("Content-Type", "application/octet-stream")
	c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
	c.Ctx.Output.Header("Expires", "0")
	c.Ctx.Output.Header("Cache-Control", "must-revalidate")
	c.Ctx.Output.Header("Pragma", "public")
	c.Ctx.ResponseWriter.Write(buffer.Bytes())
}
